package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.mall.dto.OmsOrderReturnApplyResult;
import com.mall.dto.OmsReturnApplyQueryParam;
import com.mall.dto.OmsUpdateStatusParam;
import com.mall.entity.*;
import com.mall.mapper.*;
import com.mall.service.OmsOrderReturnApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单退货申请 服务实现类
 * </p>
 *
 * @author zzx
 * @since 2022-06-15
 */
@Service
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply> implements OmsOrderReturnApplyService {

    @Autowired
    private OmsOrderReturnApplyMapper orderReturnApplyMapper;
    @Autowired
    private OmsOrderMapper orderMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PmsBrandMapper brandMapper;

    @Override
    public List<OmsOrderReturnApply> list(OmsReturnApplyQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return orderReturnApplyMapper.getList(queryParam);
    }

    @Override
    public int delete(List<Long> ids) {
        QueryWrapper<OmsOrderReturnApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids).eq("status", 3);
        return remove(queryWrapper) ? 1 : 0;
    }

    @Override
    public int updateStatus(Long id, OmsUpdateStatusParam statusParam) {
        Integer status = statusParam.getStatus();
        OmsOrderReturnApply returnApply = new OmsOrderReturnApply();
        if(status.equals(1)){
            //确认退货
            returnApply.setId(id);
            returnApply.setStatus(1);
            returnApply.setReturnAmount(statusParam.getReturnAmount());
            returnApply.setCompanyAddressId(statusParam.getCompanyAddressId());
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else if(status.equals(2)){
            //完成退货
            returnApply.setId(id);
            returnApply.setStatus(2);
            returnApply.setReceiveTime(new Date());
            returnApply.setReceiveMan(statusParam.getReceiveMan());
            returnApply.setReceiveNote(statusParam.getReceiveNote());
        }else if(status.equals(3)){
            //拒绝退货
            returnApply.setId(id);
            returnApply.setStatus(3);
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else{
            return 0;
        }
        return updateById(returnApply) ? 1 : 0;
    }

    @Override
    public OmsOrderReturnApplyResult getItem(Long id) {
        return orderReturnApplyMapper.getDetail(id);
    }

    @Override
    public int create(Long orderId, Long productId, String reason, String description,Long memberId) {
        //用orderId查询订单信息
        OmsOrder order = orderMapper.selectById(orderId);
        //用productId查询商品信息
        PmsProduct product = productMapper.selectById(productId);
        //用memberId查询会员信息
        User user = userMapper.selectById(memberId);
        //用productId查询品牌信息
        PmsBrand brand = brandMapper.selectById(product.getBrandId());
        //创建退货申请信息
        OmsOrderReturnApply returnApply = new OmsOrderReturnApply();
        returnApply.setOrder(order);
        returnApply.setProduct(product);
        returnApply.setUser(user);
        returnApply.setCreateTime(new Date());
        returnApply.setReturnAmount(order.getPayAmount());
        returnApply.setStatus(0);
        returnApply.setProductBrand(brand.getName());
        returnApply.setReason(reason);
        returnApply.setDescription(description);
        //将returnApply插入数据库
        return orderReturnApplyMapper.insert(returnApply);
    }
}
